﻿using System;
using System.Collections.Generic;
using System.IO;
using Cvk.Banner;

namespace BatDongSan.Manager
{
    public partial class ManBannerUpdate : System.Web.UI.Page
    {
        public static int _idc, _id;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _idc = Convert.ToInt32(Request.QueryString["idc"]);
                _id = Convert.ToInt32(Request.QueryString["id"]);
                LoadCat();
                if (_idc > 0)
                    ddlVitri.SelectedValue = Convert.ToString(_idc);
                if (_id > 0)
                    LoadInfo(_id);
                else btnUpdate.Text = "Thêm mới";
            }
        }
        protected void LoadCat()
        {
            try
            {
                List<Cvk_BannerCategory> list = BannerBll.claCategory.GetList(Utility.strConnect);
                if (list.Count > 0)
                {
                    ddlVitri.DataSource = list;
                    ddlVitri.DataTextField = "Name";
                    ddlVitri.DataValueField = "CategoryId";
                    ddlVitri.DataBind();
                }
            }
            catch { }
        }
        public void LoadInfo(int id)
        {
            try
            {
                //Load Vietnammes
                Cvk_Banner info = new Cvk_Banner();
                info = BannerBll.claBanner.GetInfo(id, Utility.strConnect);
                if (info != null)
                {
                    ckbActive.Checked = Convert.ToBoolean(info.IsActive);
                    txtName.Text = info.Title;
                    txtLink.Text = info.Link;
                    ddlFile.SelectedValue = Convert.ToString(info.FileType);
                    HiddenField1.Value = info.FileName;
                    txtWidth.Text = Convert.ToString(info.Width);
                    txtHeight.Text = Convert.ToString(info.Height);
                }
            }
            catch { }
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtName.Text) || string.IsNullOrEmpty(txtLink.Text))
                {
                    lbStatus.Text = "Vui lòng nhập đầy đủ thông tin cần thiết.";
                    txtName.Focus();
                    return;
                }
                //Save Info Vietnammes
                Cvk_Banner info = new Cvk_Banner();
                info.BannerId = _id;
                info.CategoryId = Convert.ToInt32(ddlVitri.SelectedValue);
                info.Title = txtName.Text;
                info.Link = txtLink.Text;
                info.IsActive = ckbActive.Checked;
                info.FileType = Convert.ToInt32(ddlFile.SelectedValue);
                info.Width = Convert.ToInt32(txtWidth.Text);
                info.Height = Convert.ToInt32(txtHeight.Text);

                #region Upload File
                string fileName1 = string.Empty;
                string filePath1 = string.Empty; //Server.MapPath(string.Format("~/Uploads/Banner/"));
                try
                {
                    if (FileUpload1.HasFile)
                    {
                        bool temp = false;
                        if (info.FileType == 1)
                        {
                            if (cvk.AppCode.WebUtility.IsValidFile(FileUpload1.FileName, ".jpg", ".jpeg", ".gif", ".bmp", ".png") && FileUpload1.FileContent.Length <= 3145728)
                                temp = true;
                            filePath1 = Server.MapPath(string.Format("~/Uploads/Banner/Img/"));
                        }
                        else if (info.FileType == 2)
                        {
                            if (cvk.AppCode.WebUtility.IsValidFile(FileUpload1.FileName, ".swf") && FileUpload1.FileContent.Length <= 3145728)
                                temp = true;
                            filePath1 = Server.MapPath(string.Format("~/Uploads/Banner/flash/"));
                        }
                        if (!Directory.Exists(filePath1))
                        {
                            Directory.CreateDirectory(filePath1);
                        }
                        if (temp) //3M
                        {
                            fileName1 = cvk.AppCode.WebUtility.GetDateTimeStringFileName(FileUpload1.FileName);
                            //cvk.AppCode.WebUtility.UploadImage(FileUpload1, filePath1, fileName1, 150, 118);
                            FileUpload1.SaveAs(filePath1 + fileName1);
                        }
                        else
                        {
                            lbStatus.Text = "File Upload không hợp lệ, vui lòng chọn File khác.";
                            return;
                        }
                    }
                    else if (_id == 0)
                    {
                        lbStatus.Text = "Vui lòng chọn File.";
                        return;
                    }
                    else fileName1 = HiddenField1.Value;
                }
                catch
                { }
                info.FileName = fileName1;
                #endregion

                if (_id > 0)
                {
                    BannerBll.claBanner.Update(info, Utility.strConnect);
                    lbStatus.Text = "Quá trình cập nhật thành công.";
                }
                else
                {
                    info.IsDelete = false;
                    info.CreateDate = DateTime.Now;
                    info.PostUser = Request.Cookies["mrvuManager"]["Username"].ToString();
                    BannerBll.claBanner.Insert(info, Utility.strConnect);
                    lbStatus.Text = "Quá trình thêm mới thành công.";
                }
                Reset();
            }
            catch (Exception ex)
            {
                lbStatus.Text = ex.Message;
            }
        }
        protected void btnReset_Click(object sender, EventArgs e)
        {
            Reset();
        }
        public void Reset()
        {
            txtName.Text
            = txtLink.Text = string.Empty;
            ckbActive.Checked = false;
            _id = 0;
            btnUpdate.Text = "Thêm mới";
        }
    }
}
